import os
import yaml
import requests
from request_Util import Requests_Util
import sys
import os
#sys.path.append(os.getcwd())
from yaml_util import Yaml_Util
import pytest
import json

class Login:
    def login(self):
        header = {'content-type': 'application/json','accept':'*/*'}
        data = {'mobile':13711110895,'mobileCode':888888}
        res = Requests_Util().send_request('post','https://hysz-test.huayunshuzi.com/flagAppApi/api/flagUser/mobileCodeLogin',header,data)
        result = json.loads(res)
        tokenvalue = result['result']['token']
        with open(os.path.abspath(os.path.dirname(os.getcwd())) + '/testcase/' + 'extract.yml', 'r', encoding='utf-8') as f:
            doc = yaml.load(stream=f,Loader=yaml.FullLoader)
            print(doc)
            print(os.path.abspath(os.path.dirname(os.getcwd()))+ '/testcase/' + 'extract.yml')
            print(type(doc))
            if 'Authorization' in doc.keys() and tokenvalue != doc['Authorization']:
                doc['Authorization'] = tokenvalue
                with open(os.path.abspath(os.path.dirname(os.getcwd())) + '/testcase/' + 'extract.yml', mode='w', encoding='utf-8') as f:#如果已经存在就直接写入覆盖
                    yaml.dump(data=doc, stream=f, allow_unicode=True,default_flow_style=False)
            else:
                with open(os.path.abspath(os.path.dirname(os.getcwd())) + '/testcase/' + 'extract.yml', mode='a', encoding='utf-8') as f:#如果不存在就直接追加
                    yaml.dump(data=data, stream=f, allow_unicode=True,default_flow_style=False)
        print(tokenvalue)
        return tokenvalue
if __name__ == '__main__':
    Login().login()
